package com.turing.post.salesmanage.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.DepositOrderActionDetail;
import com.turing.post.salesmanage.util.DepositOrderActionDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 充值单状态迁移明细（DepositOrderActionDetail）
 * 
 * @author zxp
 * @Jan 17, 2013
 */

@Stateless(mappedName = "salesmanage.impl.DepositOrderActionDetailDaoImpl")
@Remote
@Local
public class DepositOrderActionDetailDaoImpl implements
		DepositOrderActionDetailDao {

	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<DepositOrderActionDetail> getDepositOrderActionDetailList(
			BigInteger depositOrderId, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "select * from DepositOrderActionDetail where depositorderid =?1 order by actiontime";
		Query query = _em
				.createNativeQuery(sql, DepositOrderActionDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult).setParameter(1, depositOrderId);
		return query.getResultList();
	}

	@Override
	public BigInteger getDepositOrderActionDetailTotal(BigInteger depositOrderId) {
		String sql = "select nvl(count(*),0) from DepositOrderActionDetail where depositorderid =?1";
		Query query = _em.createNativeQuery(sql).setParameter(1, depositOrderId);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertDepositOrderActionDetail(DepositOrderActionDetail doad)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, doad);
	}
}